package ua.kiev.tosik.price.modules.search.conf;

import org.apache.log4j.Logger;
import ua.kiev.tosik.price.conf.AppConfig;

import java.util.Locale;
import java.util.ResourceBundle;

public class ModuleConfig {

	private static ModuleConfig ourInstance;
	private static final String KEYWORDS_FILE = "ua.kiev.tosik.price.modules.search.conf.keywords";
	private static Logger logger = Logger.getLogger("ua.kiev.tosik.price.modules.search.conf");
	private ResourceBundle resourceBundle;
	private Locale locale;
	private boolean status = false;
	private String message;

	public static ModuleConfig getInstance() {
		if ( ourInstance == null ) {
			ourInstance = new ModuleConfig();
		}
		return ourInstance;
	}

	private ModuleConfig() {
		logger.debug("Init translations");
		locale = new Locale(AppConfig.getInstance().get("app.language"));
		try {
			resourceBundle = ResourceBundle.getBundle(KEYWORDS_FILE, locale);
			status = true;
		} catch (Exception e) {
			status = false;
			message = e.getLocalizedMessage();
			logger.fatal(e.getLocalizedMessage());
		}
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public String get(String key) {
		return resourceBundle.getString(key);
	}

	public boolean isStatus() {
		return status;
	}

	public String getMessage() {
		return message;
	}
}
